import fetch from '../../utils/fetch.js'
Page({
  data:{
    id:'',
    current:1,
    pageSize:10,
    shopList: [],
    hasMore: true
  },
  onLoad(query){
    this.data.id = query.id
    this.setData(this.data);
  },
  onShow(){
    this.getShopList()
  },
  onReady(){
    fetch({
      url:`categories/${this.data.id}`
    }).then(res =>{
      //设置当前页标题
      wx.setNavigationBarTitle({
        title: res.data.name,
      })
    })
    
  },
  //页面触底函数
  onReachBottom(){
    if(!this.data.hasMore) return
    this.data.current++
    this.setData(this.data)
    this.getShopList()
  },
  getShopList(page){
    let {id,current,pageSize,shopList} = this.data
    fetch({
      url: `categories/${this.data.id}/shops?_page=${this.data.current}&_limit=${this.data.pageSize}`
    }).then(res => {
      this.data.shopList = [...shopList,...res.data]
      

    //判断是否还有更多的数据
    let total = res.header['X-Total-Count']
    this.data.hasMore =this.data.current < Math.ceil(total/this.data.pageSize)
    this.setData(this.data)
    })
  },
  onPullDownRefresh(){
    this.data.current = 1
    this.data.shopList = []
    this.setData(this.data)
    this.getShopList();
  }
})